﻿using System.Web;

using Tools.Json;

using SY.WebSite.BLL;

namespace SY.WebSite.Web.Handler
{
    public class UserHandler : InternalWebHandler, IHttpHandler
    {
        public new void ProcessRequest(HttpContext context)
        {
            base.ProcessRequest(context);

            switch (Action)
            {
                case "reset":
                    ReSet();
                    break;
                case "logout":
                    LogOut();
                    break;
            }

            context.Response.End();
        }

        public void LogOut() {
            Auth.SetLogOut();
            Response.Write(new JsonTransfer().Add("success","log out successful."));
        }

        public void ReSet()
        {
            string oldPassword = Tools.Web.Request.GetFormString("oldPassword");
            string newPassword = Tools.Web.Request.GetFormString("newPassword");

            if (string.IsNullOrEmpty(oldPassword) || string.IsNullOrEmpty(newPassword))
                BreakResponse();

            CurrentUser.Password = newPassword;
            int result = BizUser.UpdatePassword(CurrentUser, oldPassword);
            if (result > 0)
            {
                Response.Write(new JsonTransfer().Add("success", ""));
                return;
            }
            BreakResponse("原始密码错误");
        }
    }
}